import request from '../utils/request'

export const cateAdd = (cateObj)=>{ //新增分类
  return request.post('/classes/CakeCate',cateObj)
}

export const cateGet = ()=>{ //分类列表
  return request.get('/classes/CakeCate')
}


export const bannerAdd = (bannerObj)=>{ //新增轮播
  return request.post('/classes/CakeBanner',bannerObj)
}

export const bannerGet = ()=>{ //轮播列表
  return request.get('/classes/CakeBanner')
}

export const bannerDel = (objectId)=>{ //轮播删除
  return request.delete(`/classes/CakeBanner/${objectId}`)
}

export const bannerUpdate = (objectId,bannerObj)=>{ //更新轮播
  return request.put(`/classes/CakeBanner/${objectId}`,bannerObj)
}

export const cakeGet = (params,sort='ascend' )=>{ //蛋糕列表
  for(let attr in params){ //剔除条件对象中，值为null的字段
    if(!params[attr]||attr==='pageSize'||attr=='current'||attr=='created_at'){
      delete params[attr];
    }
  }
  let wh = JSON.stringify(params)
  let sortstr = sort === 'ascend' ? '-' : ''
  // return request.get(`/classes/CakeList?where={"name":"心动回音"}`)
  return request.get(`/classes/CakeList?where=${wh}&order=${sortstr}price`)
}

export const CakeBatch = (list,values)=>{ //蛋糕商品批量录入,list是目标网站的数据包，values是自己录入的数据（分类、详情）
  let requests = list.map(item=>{
    return {
      method:'POST',
      path:'/1.1/classes/CakeList',
      body:{
        ...item,
        ...values
      }
    }
  })
  return request.post('/batch',{requests})
}

export const chartGet = (type)=>{
  return request.get(`/classes/CakeChart?where={"type":"${type}"}`)
}

export const areaAdd = (areaObj)=>{ //新增配送范围
  return request.post('/classes/CakeArea',areaObj)
}
export const areaGet = (city="重庆市")=>{ //获取配送范围
  return request.get(`/classes/CakeArea?where={"city":"${city}"}`)
}